const cheerio = require('cheerio');
const fs = require('fs-extra');
const utils  = require('../lib/utils');

const getUrl = function(num) {
    return `http://www.touxiang.cn/feizhuliu/nansheng/index_${num}.html`;
}

const getImgSrc = function(imgArr) {
    let arr = [];
    imgArr.each((i, elem) => {
        let src = $(elem).attr("src");
        arr.push(src);
    });
    return arr;
}

const main = async function () {
    let imgArr = [];
    const len = 66;
    for (let index = 1; index < len + 1; index++) {
        console.log(index);
        let reqResult = await utils.uget(getUrl(index));
        $ = cheerio.load(reqResult.body);
        let arr = getImgSrc($(".f_lifl.m_txul").find("img"))
        imgArr = imgArr.concat(arr);
    }
    fs.writeFileSync('./male_avatars.json', JSON.stringify(imgArr, null, 4));
}

main();